package com.bestv.search.common.service.impl;

import com.bestv.search.common.dao.PersonDao;
import com.bestv.search.common.dataSource.DataSourceKey;
import com.bestv.search.common.model.Info;
import com.bestv.search.common.model.Person;
import com.bestv.search.common.model.PersonExtend;
import com.bestv.search.common.model.PersonProduct;
import com.bestv.search.common.service.PersonManager;

import java.util.*;

public class PersonManagerImpl implements PersonManager {
    private PersonDao personDao;

    public void saveOrUpdate(DataSourceKey key, List<Person> list) {
        for (Person p : list) {
            personDao.saveOrUpdate(p);
        }
    }

    public void updateAvatar(DataSourceKey key, String personId, byte[] avatar) {
        personDao.updateAvatar(personId, avatar);
    }

    public List<String> getAllName(DataSourceKey key, int start, int limit) {
        return personDao.getAllName(start, limit);
    }

    public int getCount(DataSourceKey key) {
        return personDao.getCount();
    }

    public void saveProductInfoReferenceList(DataSourceKey key, List<Map.Entry<String, Long>> mapping) {
        for (Map.Entry<String, Long> entry : mapping) {
            if (!personDao.existProductInfoReference(entry.getKey(), entry.getValue())) {
                personDao.saveProductInfoReference(entry.getKey(), entry.getValue());
            }
        }
    }

    public Set<String> getPersonIdByInfoId(DataSourceKey key, long info_id) {
        Set<String> personIds = new HashSet<String>();
        List<String> personIdList = personDao.getPersonIdByInfoId(info_id);
        for (String id : personIdList) {
            personIds.add(id);
        }
        return personIds;
    }

    public Set<String> getPersonIdByInfos(DataSourceKey key, List<Info> list) {
        Set<String> personIds = new HashSet<String>();
        for (Info info : list) {
            List<String> personIdList = personDao.getPersonIdByInfoId(info.getId());
            for (String id : personIdList) {
                personIds.add(id);
            }
        }

        return personIds;
    }

    public List<PersonExtend> getPersonByUpdateTime(DataSourceKey key, Date updateTime, int start, int limit) {
        return personDao.getPersonByUpdateTime(updateTime, start, limit);
    }

    public List<PersonExtend> getPersonByName(DataSourceKey key, String name, String[] nationalityArray, int start,
                                              int limit) {
        return personDao.getPersonByName(name, nationalityArray, start, limit);
    }

    public int getPersonCountByName(DataSourceKey key, String name, String[] nationalityArray) {
        return personDao.getPersonCountByName(name, nationalityArray);
    }

    public int getPersonCountByUpdateTime(DataSourceKey key, Date updateTime) {
        return personDao.getPersonCountByUpdateTime(updateTime);
    }

    public PersonExtend getPersonById(DataSourceKey key, String personId) {
        return personDao.getPersonById(personId);
    }

    public List<PersonExtend> getPersonById(DataSourceKey key, Set<String> personIds) {
        List<PersonExtend> persons = new ArrayList<PersonExtend>();
        for (String id : personIds) {
            PersonExtend p = personDao.getPersonById(id);
            if (p != null) {
                persons.add(p);
            }
        }
        return persons;
    }

    public List<PersonProduct> getPersonProductsByPersonId(DataSourceKey key, String personId) {
        return personDao.getPersonProductsByPersonId(personId);
    }

    public PersonDao getPersonDao() {
        return personDao;
    }

    public void setPersonDao(PersonDao personDao) {
        this.personDao = personDao;
    }
}
